//
//  WokerViewController.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/20.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "WokerViewController.h"
#import "CompanyDetailViewController.h"

#import "HBHWebViewCell.h"
#import "WorkerCell.h"
#import "HDJDWorkDetailCell.h"
@interface WokerViewController ()<UITableViewDelegate,UITableViewDataSource,UIWebViewDelegate>

@property (nonatomic,strong)UITableView *tableV;
@property (nonatomic,strong)UIButton *sendBtn;//发送按钮

@property (nonatomic,strong)NSDictionary *data;

@property (nonatomic) CGFloat webH;
@property (nonatomic, strong) UIWebView *web;
@end

@implementation WokerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatView];
    [self getDetail];
}

-(void)creatView
{
    [self initTitleWithTitle:@"职位详情"];
    [self.view addSubview:self.tableV];
    [self.view addSubview:self.sendBtn];
}

-(UIWebView *)web
{
    if (!_web) {
        _web = [[UIWebView alloc]init];
        _web.delegate = self;
    }
    return _web;
}

//Web加载完成
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    //计算webView的高
    CGRect frame = webView.frame;
    frame.size.width = SCREENW;
    frame.size.height = 1;
    webView.scrollView.scrollEnabled = NO;
    webView.frame = frame;
    frame.size.height = webView.scrollView.contentSize.height;
    webView.frame = frame;
    _webH = frame.size.height;
    [_tableV reloadData];
}

-(UITableView *)tableV
{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREENW, SCREENH-64-70*screenW_6) style:UITableViewStyleGrouped];
        _tableV.backgroundColor = backColor;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.delegate = self;
        _tableV.dataSource = self;
    }
    return _tableV;
}
#pragma Mark TableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return 3;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (!_data) {
        return 0;
    }
    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10*screenW_6;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    __weak typeof(self) weakSelf = self;
    if (indexPath.section == 0) {
        WorkerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WorkerCell"];
        if (cell == nil) {
            cell = [[WorkerCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"WorkerCell"];
        }
        //加载cell
        [cell loadCellWithData:_data];
        cell.blcok = ^(){
            [weakSelf goToCompany];
        };
        return cell;
    }else{
        if (indexPath.row == 0) {
            UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
            if (cell == nil) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
            }
            cell.textLabel.text = @"职位描述";
            cell.textLabel.textColor = TextBlack;
            cell.textLabel.font = [UIFont systemFontOfSize:15*screenW_6];
//            [cell addSubview:[ToolViewVC addCellLineLab:CGRectMake(15*screenW_6, cell.frame.size.height-1, SCREENW-15*screenW_6, 1*screenW_6)]];
            return cell;
        }else if(indexPath.row == 1){
            HDJDWorkDetailCell*cell = [tableView dequeueReusableCellWithIdentifier:@"HDJDWorkDetailCell"];
            if (cell == nil) {
                cell = [[HDJDWorkDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HDJDWorkDetailCell"];
            }
            [cell loadCellWithData:_data];
            return cell;
        }else{
            HBHWebViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HBHWebViewCell"];
            if (!cell) {
                cell = [[HBHWebViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HBHWebViewCell"];
            }
            [cell loadCellWithWeb:self.web];
            return cell;
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 186.5*screenW_6;
    }else if (indexPath.section == 1 && indexPath.row == 0){
        return 50*screenW_6;
    }else if (indexPath.section == 1 && indexPath.row == 1){
//        NSString *detail = [NSString stringWithFormat:@"岗位职责\n%@",_data[@"positionDescribe"]];
        return 83*screenW_6;
    }else if (indexPath.section == 1 && indexPath.row == 2){
        return _webH;
    }
    return 0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    [self.navigationController pushViewController:[CompanyDetailViewController new] animated:YES];
}
-(UIButton *)sendBtn//发送按钮
{
    if (!_sendBtn) {
        _sendBtn = [TomamaUI createButton:UIButtonTypeCustom Frame:CGRectMake(10*screenW_6,SCREENH-64-60*screenW_6, SCREENW-20*screenW_6, 45*screenW_6) targ:self sel:@selector(saveSelectBtn) titleColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:17*screenW_6] title:@"发送简历" image:@"" backGroundImage:@""];
        _sendBtn.backgroundColor = greenColor;
        [_sendBtn.layer setCornerRadius:5*screenW_6];
        _sendBtn.layer.masksToBounds = YES;
    }
    return _sendBtn;
}

-(void)saveSelectBtn//发送按钮点击事件
{
    if (![Storage isLogin]) {
        [Storage goToLogin];
    }else{
        [HudHelper showProgressHudWithMessage:@"" toView:self.view];
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [HttpRequest PostrequestUrl:@"/recruit/addDeliveryRecord" Dic:@{@"jdUser.id":[Storage getToken],@"jdCompanyInfo.id":_data[@"companyId"],@"jdPosition.id":_data[@"id"]} completion:^(NSDictionary *responseDic) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [HudHelper hideHudToView:self.view];
                    if ([responseDic[@"success"] boolValue]) {
                        [TomamaUI creatAlertWithTarget:self message:@"投递成功" timeInterval:2];
                    }else{
                        [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                    }
                });
            }];
        });
    }
}

-(void)goToCompany
{
    CompanyDetailViewController *vc = [CompanyDetailViewController new];
    vc.companyId = _data[@"companyId"];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - request
-(void)getDetail
{
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/recruit/positionDetails" Dic:@{@"id":_wordId} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {
                    _data = responseDic[@"data"];
                    NSString *content = [NSString stringWithFormat:@"<head><style>img{max-width:%fpx ;height: auto !important}</style></head>%@",SCREENW - 10,_data[@"positionDescribe"]];
                    [self.web loadHTMLString:content baseURL:nil];
                    [_tableV reloadData];
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
            });
        }];
    });
}

@end
